<!DOCTYPE html>
<html>
<head>
    <title>登录测试</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
        .form-group { margin: 15px 0; }
        label { display: block; margin-bottom: 5px; }
        input { width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px; }
        button { background: #1890ff; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
        .result { margin: 20px 0; padding: 15px; border-radius: 4px; }
        .success { background: #f6ffed; border: 1px solid #b7eb8f; }
        .error { background: #fff2f0; border: 1px solid #ffccc7; }
    </style>
</head>
<body>
    <h1>登录测试</h1>
    
    <form id="loginForm">
        <div class="form-group">
            <label>邮箱:</label>
            <input type="email" id="email" value="test@admin.com" required>
        </div>
        <div class="form-group">
            <label>密码:</label>
            <input type="password" id="password" value="admin123" required>
        </div>
        <button type="submit">登录</button>
        <button type="button" onclick="register()">注册</button>
    </form>
    
    <div id="result"></div>
    
    <div>
        <h3>操作:</h3>
        <button onclick="goToDashboard()">跳转到Dashboard</button>
        <button onclick="clearToken()">清除Token</button>
    </div>
    
    <script>
        const resultDiv = document.getElementById('result');
        
        function showResult(message, isSuccess = true) {
            resultDiv.innerHTML = `<div class="result ${isSuccess ? 'success' : 'error'}">${message}</div>`;
        }
        
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ email, password })
                });
                
                const data = await response.json();
                
                if (data.success && data.data.token) {
                    localStorage.setItem('admin_token', data.data.token);
                    showResult(`登录成功！Token已保存: ${data.data.token.substring(0, 50)}...`, true);
                } else {
                    showResult(`登录失败: ${data.message}`, false);
                }
            } catch (error) {
                showResult(`请求失败: ${error.message}`, false);
            }
        });
        
        async function register() {
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        email,
                        password,
                        username: 'testadmin',
                        first_name: 'Test',
                        last_name: 'Admin'
                    })
                });
                
                const data = await response.json();
                
                if (data.success && data.data.token) {
                    localStorage.setItem('admin_token', data.data.token);
                    showResult(`注册成功！Token已保存: ${data.data.token.substring(0, 50)}...`, true);
                } else {
                    showResult(`注册失败: ${data.message}`, false);
                }
            } catch (error) {
                showResult(`请求失败: ${error.message}`, false);
            }
        }
        
        function goToDashboard() {
            window.location.href = '/dashboard';
        }
        
        function clearToken() {
            localStorage.removeItem('admin_token');
            showResult('Token已清除', true);
        }
        
        // 页面加载时显示当前token状态
        window.onload = function() {
            const token = localStorage.getItem('admin_token');
            if (token) {
                showResult(`当前Token: ${token.substring(0, 50)}...`, true);
            } else {
                showResult('未找到Token', false);
            }
        };
    </script>
</body>
</html>